package Verifier.ByteTree;

/**
 * @author ofekp
 */
public class Leaf extends ByteTreeElement{
	byte[] dataByteArray;
	String dataByteString;
	String byteString;
	int numOfBytes;
	
	/**
	 * The class constructor which creates a leaf that can contain data.
	 * @param bArr a byte array to make the data
	 * @param parent the parent of this leaf
	 */
	// validity: the data size in bytes is the data string length divided by 2
	public Leaf(byte[] bArr, Node parent) {
		super(parent);
		this.dataByteArray = bArr;
		this.numOfBytes = bArr.length;
		this.dataByteString = ByteTree.byteArrayToByteString(bArr);
		this.byteString = "01" + numTo8Char(numOfBytes) + dataByteString;
		
		// tests:
		//assert(dataByteString.length() % 2 == 0);
		//assert(dataByteString.length() % 2 == dataByteArray.length);
	}
	
	/**
	 * @return true as this is a leaf
	 */
	public Boolean isLeaf() {
		return true;
	}

	/**
	 * @return the number of bytes in the data of this leaf
	 */
	public int getDataLength() {
		return dataByteArray.length;
	}
	
	/**
	 * @return the data of this leaf as byte array
	 */
	public byte[] getDataByteArray() {
		return dataByteArray;
	}

	/**
	 * @return the data of this leaf as byte string
	 */
	public String getDataByteString() {
		return dataByteString;
	}
	
	/**
	 * @return 	<pre>the byte string that represent that leaf
	 * 			including the '01' prefix, the number of bytes of data,
	 * 			and the data that follows all concatenated together.</pre>
	 */
	@Override
	public String toByteString() {
		return byteString;
	}
}
